//
//  CameraViewCell.m
//  YSSmartHome
//
//  Created by yanghao on 2016/10/13.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "CameraViewCell.h"
#import "DataManager.h"
#import "LiveVideoViewController.h"
#import "UIImageView+WebCache.h"
#import "UIView+HUD.h"
#import "GlobalConst.h"

@interface CameraViewCell ()
<
UIAlertViewDelegate
>
@property (nonatomic, strong)UIImageView    *m_imgPicView;
@property (nonatomic, strong)UILabel        *m_lblTitle;
@property (nonatomic, strong)UIButton       *m_btnDelDev;
@property (nonatomic, strong)UIView         *m_viewBottom;
@property (nonatomic, strong)UIView         *m_viewTop;
@property (nonatomic, strong)UIButton       *m_btnLive;
@property (nonatomic, strong)UIButton       *m_btnVideo;
@property (nonatomic, strong)UIButton       *m_btnCloud;
@property (nonatomic, strong)UIButton       *m_btnMessage;
@property (nonatomic, strong)UIButton       *m_btnSetting;
@end
@implementation CameraViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self initSubviews];
    }
    return self;
}
- (void)initSubviews
{
    self.contentView.backgroundColor = kBgColor;
    [self.contentView addSubview:self.m_viewTop];
    [self.m_viewTop addSubview:self.m_lblTitle];
    [self.m_viewTop addSubview:self.m_btnDelDev];
    [self.contentView addSubview:self.m_imgPicView];
    [self.contentView addSubview:self.m_viewBottom];
    [self.m_viewBottom addSubview:self.m_btnLive];
    [self.m_viewBottom addSubview:self.m_btnVideo];
    [self.m_viewBottom addSubview:self.m_btnCloud];
    [self.m_viewBottom addSubview:self.m_btnMessage];
    [self.m_viewBottom addSubview:self.m_btnSetting];
    self.textLabel.font = [UIFont systemFontOfSize:12.0f];
    self.accessoryType = UITableViewCellAccessoryNone;
    self.selectionStyle = UITableViewCellSelectionStyleBlue;
    self.selectionStyle = NO;
    [self layoutPageSubviews];
}
- (void)layoutPageSubviews
{
    [self.m_viewTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.top.leading.equalTo(self.contentView);
        make.height.equalTo(kIphoneSize_Height(40));
    }];
    [self.m_lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.m_viewTop);
        make.leading.equalTo(10);
    }];
    [self.m_btnDelDev mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.m_lblTitle);
        make.trailing.equalTo(self.m_viewTop.trailing).offset(-10);
    }];
    [self.m_imgPicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.m_viewTop.bottom);
        make.leading.trailing.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    [self.m_viewBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.contentView);
        make.height.equalTo(55);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    [self.m_btnLive mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.equalTo(self.m_viewBottom);
        make.width.equalTo(kScreenWidth / 5);
    }];
    [self.m_btnVideo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.bottom.equalTo(self.m_btnLive);
        make.leading.equalTo(self.m_btnLive.trailing);
    }];
    [self.m_btnCloud mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.bottom.equalTo(self.m_btnLive);
        make.leading.equalTo(self.m_btnVideo.trailing);
    }];
    [self.m_btnMessage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.bottom.equalTo(self.m_btnLive);
        make.leading.equalTo(self.m_btnCloud.trailing);
    }];
    [self.m_btnSetting mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.bottom.equalTo(self.m_btnLive);
        make.leading.equalTo(self.m_btnMessage.trailing);
    }];

}
- (void)setCamera:(ZTLCCamera *)camera
{
    _camera = camera;
    self.m_lblTitle.text = [camera getDeviceName];
    [self.m_imgPicView sd_setImageWithURL:[NSURL URLWithString:camera.cameraDeviceInfo.picUrl] placeholderImage:[UIImage imageNamed:@"common_defaultcover.png"]];
}

#pragma mark - event response
-(void)bottomViewBtnClick:(UIButton *)btn
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cameraViewLCCamera:clickType:)])
    {
        CameraButtonType type = CameraButtonTypeUnKonw;
        if (btn == self.m_btnLive) {
            type = CameraButtonTypeLive;
        }else if (btn == self.m_btnVideo) {
            type = CameraButtonTypeVideo;
        }else if (btn == self.m_btnCloud){
            type = CameraButtonTypeCloud;
        }else if (btn == self.m_btnMessage){
            type = CameraButtonTypeMessage;
        }else if (btn == self.m_btnSetting){
            type =CameraButtonTypeSetting;
        }
        [self.delegate cameraViewLCCamera:self.camera clickType:type];
    }
}

-(void)onDelete:(id)sender
{
    UIAlertView *alertDelView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"你是否要删除?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertDelView show];
    return;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        if (![self.camera unbundling])
        {
            UIView* view = [UIApplication sharedApplication].windows.lastObject;
            [view yh_showMsgHUD:@"解绑失败"];
            return;
        }
        NSMutableArray* array = [NSMutableArray array];
        XAITYPEAPSN apsn = self.camera.apsn;
        XAITYPELUID luid = self.camera.luid;
        XAITLV* tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_BIN_APSN bytes:&apsn length:sizeof(apsn)];
        [array addObject:tlv];
        tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_BIN_LUID bytes:&luid length:sizeof(luid)];
        [array addObject:tlv];
        NSError* error = [kMQTTSessionManager.xai  sendCMD:0x01 toApsn:kMQTTSessionManager.currentUser.currentBindApsn toLuid:kGATEWAYLUID datas:array];
        if (error.code == 0)
        {
            debugLogInfo(@"删除成功");
        }
        else
        {
            debugLogInfo(@"删除失败%@",error.localizedDescription);
        }
    }
}
#pragma mark - getters and setters

- (UIImageView *)m_imgPicView
{
    if (!_m_imgPicView)
    {
        _m_imgPicView = [[UIImageView alloc]init];
        _m_imgPicView.userInteractionEnabled = YES;
    }
    return _m_imgPicView;
}

- (UILabel *)m_lblTitle
{
    if (!_m_lblTitle)
    {
        _m_lblTitle = [[UILabel alloc]init];
        [_m_lblTitle setFont:[UIFont systemFontOfSize:14.0f]];
        _m_lblTitle.textColor = RGBColor(51, 51, 51);
        _m_lblTitle.backgroundColor = [UIColor whiteColor];
    }
    return _m_lblTitle;
}

- (UIButton *)m_btnDelDev
{
    if (!_m_btnDelDev)
    {
        _m_btnDelDev = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *imgDelDev = [UIImage imageNamed:@"security_icon_trash"];
        [_m_btnDelDev setImage:imgDelDev forState:UIControlStateNormal];
        [_m_btnDelDev addTarget:self action:@selector(onDelete:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_btnDelDev;
}

- (UIView *)m_viewBottom
{
    if (!_m_viewBottom) {
        _m_viewBottom = [[UIView alloc]init];
        _m_viewBottom.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    }
    return _m_viewBottom;
}
- (UIView *)m_viewTop
{
    if (!_m_viewTop) {
        _m_viewTop = [[UIView alloc]init];
        _m_viewTop.backgroundColor = [UIColor whiteColor];
    }
    return _m_viewTop;
}
- (UIButton *)m_btnLive
{
    if (!_m_btnLive) {
        _m_btnLive = [UIButton buttonWithType:UIButtonTypeCustom];
        [_m_btnLive setImage:[UIImage imageNamed:@"security_icon_live"] forState:UIControlStateNormal];
        _m_btnLive.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        _m_btnLive.backgroundColor = [UIColor clearColor];
        [_m_btnLive addTarget:self action:@selector(bottomViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_btnLive;
}
- (UIButton *)m_btnVideo
{
    if (!_m_btnVideo) {
        _m_btnVideo = [UIButton buttonWithType:UIButtonTypeCustom];
        [_m_btnVideo setImage:[UIImage imageNamed:@"security_icon_localvideo"] forState:UIControlStateNormal];
        _m_btnVideo.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        _m_btnVideo.backgroundColor = [UIColor clearColor];
        [_m_btnVideo addTarget:self action:@selector(bottomViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_btnVideo;
}
- (UIButton *)m_btnCloud
{
    if (!_m_btnCloud) {
        _m_btnCloud = [UIButton buttonWithType:UIButtonTypeCustom];
        [_m_btnCloud setImage:[UIImage imageNamed:@"security_icon_cloud"] forState:UIControlStateNormal];
        _m_btnCloud.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        _m_btnCloud.backgroundColor = [UIColor clearColor];
        [_m_btnCloud addTarget:self action:@selector(bottomViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_btnCloud;
}

- (UIButton *)m_btnMessage
{
    if (!_m_btnMessage) {
        _m_btnMessage = [UIButton buttonWithType:UIButtonTypeCustom];
        [_m_btnMessage setImage:[UIImage imageNamed:@"security_icon_message"] forState:UIControlStateNormal];
        _m_btnMessage.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        _m_btnMessage.backgroundColor = [UIColor clearColor];
        [_m_btnMessage addTarget:self action:@selector(bottomViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_btnMessage;
}

- (UIButton *)m_btnSetting
{
    if (!_m_btnSetting) {
        _m_btnSetting = [UIButton buttonWithType:UIButtonTypeCustom];
        [_m_btnSetting setImage:[UIImage imageNamed:@"security_icon_setting"] forState:UIControlStateNormal];
        _m_btnSetting.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        _m_btnSetting.backgroundColor = [UIColor clearColor];
        [_m_btnSetting addTarget:self action:@selector(bottomViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_btnSetting;
}

@end
